<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>
      </template>
      <template slot-scope="{ row }" slot="generationType">
        <span>{{ row.generationType == 'ERP' ? 'ERP同步' : 'MES新增' }}</span>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/finished-inbound-record/page" :option="option" :page="page" :query="exportParams" file-name="成品入库单">
    </export-comp>
  </basic-container>
</template>

<script>
import { getList } from '@/api/warehouse/productWarehouseRecords';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import exportComp from '@/components/export-comp';

export default {
  components: {
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'FINISH_WAREHOUSE_MANAGE',
      form: {},
      exportParams: {},
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      data: [],
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 9,
        searchLabelWidth: 130,
        dialogClickModal: false,
        border: true,
        height: 'calc(100vh - 440px)',
        viewBtn: true,
        addBtn: false,
        selection: true,
        printBtn: false,
        editBtn: false,
        delBtn: false,
        index: false,
        columnBtn: false,
        menu: true,
        searchSpan: 5,
        column: [
          {
            label: '成品入库单',
            prop: 'finishedInboundOrderNo',
            search: true,
            searchOrder: 1
          },
          {
            label: '产品编码',
            prop: 'productPartNo'
          },
          {
            label: '产品编码',
            prop: 'productPartName'
          },
          {
            label: '产品编码',
            prop: 'productPartModel'
          },
          {
            label: '生产工单',
            prop: 'workOrderNo',
            hide: true,
            search: true,
            searchOrder: 2
          },
          {
            label: '工单号',
            prop: 'workOrderNo'
          },
          {
            label: '生产批次',
            prop: 'lotNo'
          },
          {
            label: '计划入库数量',
            prop: 'inboundQuantity'
          },
          {
            label: '入库时间',
            prop: 'inboundTime'
          },
          {
            label: '操作人',
            prop: 'inboundByName'
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {};
    }
  },
  methods: {
    handleExport() {
      this.$refs.exportComp.visible = true;
      this.exportParams = this.query;
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          done();
        },
        (error) => {
          loading();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
        });
    },
    beforeOpen(done, type) {
      if (['edit', 'view'].includes(type)) {
        getSheetNoPack(this.form.id).then((res) => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
